package com.mmp.main.servlet.device;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

public class PPMVisitDisplayServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int visitMonth = WebUtil.getParamValueAsInteger(request, "visitMonth", 0);
		int visitYear = WebUtil.getParamValueAsInteger(request, "visitYear", 0);

		if (visitMonth != 0 && visitYear != 0) { // ajax request

			int deviceId = WebUtil.getParamValueAsInteger(request, "deviceId", 0);
			HospitalDevice device = dbQueryManager.findById(deviceId, HospitalDevice.class);	
			request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, device);

			HospitalDevicePpmDetail ppmVisit = new HospitalDevicePpmDetail();
			ppmVisit.setHospitalDevice(device);
			ppmVisit.setVisitMonth(visitMonth);
			ppmVisit.setVisitYear(visitYear);

			List<HospitalDevicePpmDetail> searchResults = dbQueryManager.find(ppmVisit);
			if (searchResults != null && searchResults.size() == 1)
				request.setAttribute("ppmVisit", searchResults.get(0));

			basicForward(request, response, "device/PPMVisit.jsp");
			return;
		}

		// if (request.getAttribute(Default.DEVICE_INFO_ATTR_NAME) == null)
		// request.setAttribute(Default.DEVICE_INFO_ATTR_NAME,
		// dbm.find(device).get(0));

		// dbm.destory();

		forward(request, response, "device/PPMVisit.jsp");
	}
}
